package jh.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jh.ios.forecast.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jh.activity.HsitoryActivity;
import jh.adapter.LotteryAdapter;
import jh.model.MModel;
import jh.model.MTypeModel;
import jh.utils.HttpUtils;

public class FragmentOpenNumber extends Fragment {

    private View view;
    private TextView textView;
    private ListView listView;
    private String code;
    private LotteryAdapter adapter;
    private SwipeRefreshLayout swipeRefresh;
    private ProgressBar progressBar;
    private List<MModel> list = new ArrayList<>();
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(),R.layout.fragment_open_number,null);
        code = getArguments().getString("code");

        initView();
        getData();
        return view;
    }

    private void initView(){

        progressBar = view.findViewById(R.id.progressBar);

        listView = view.findViewById(R.id.list_view);
        adapter = new LotteryAdapter(getActivity(),list,false);
        listView.setAdapter(adapter);

        swipeRefresh = view.findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.colorMain);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),HsitoryActivity.class);
                intent.putExtra("title",list.get(position).getLotteryCategoryName());
                intent.putExtra("code",list.get(position).getLotteryCategoryCode());
                startActivity(intent);
            }
        });
    }



    private void getData(){


        String url = "/api/lotteryOpenNumber/getDifferentLotteryCategoryNewestOpenNumber";

        Map<String,String> map = new HashMap<>();
        map.put("parentLotteryCategoryCode",code);

        HttpUtils.POST(url, map, new HttpUtils.HttpResponse() {
            @Override
            public void callback(JSONObject object, String msg, Exception e) {
                swipeRefresh.setRefreshing(false);
                progressBar.setVisibility(View.GONE);
                swipeRefresh.setVisibility(View.VISIBLE);
                if (e == null){
                    if (msg == null){
                        Log.i("json == ",object.toString());
                        JSONArray jsonArray = null;
                        try {
                            list.clear();
                            jsonArray = object.getJSONArray("data");
                            GsonBuilder builder = new GsonBuilder();
                            Gson gson = builder.create();
                            List<MModel> array = gson.fromJson(jsonArray.toString(),new TypeToken<List<MModel>>(){}.getType());

                            list.addAll(array);
                            adapter.notifyDataSetChanged();

                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });
    }

}
